package com.base.balibrary.utils.http;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * BAndroid
 * 设置管理httpclient
 * Created by YichenZ on 2015/8/4 11:13.
 * http://blog.csdn.net/heng615975867/article/details/9012303
 */
public class BaseHttpClient {

    private static Object obj =new Object();
    private static BaseHttpClient instance;

    public static BaseHttpClient getInstance(){
        if(instance==null){
            synchronized (obj){
                if(instance==null){
                    instance=new BaseHttpClient();
                }
            }
        }
        return  instance;
    }

    private static HttpClient httpClient;
    private static HttpPost defaultHttpPost;
    private static HttpPost gZipHttpPost;

    private static final int DEFAULT_MAX_CONNECTIONS = 30;
    private static final int DEFAULT_HOST_CONNECTIONS = 10;
    private static final int DEFAULT_TIMEOUT = 1000;
    private static final int DEFAULT_SOCKET_TIMEOUT = 20 * 1000;
    private static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;

    private static final String DEFAULT_HTTP_POST_CONTENT_TYPE="application/json";

    private BaseHttpClient(){

    }

    /**
     * 获取一个HttpClient对象
     * @return
     */
    public static synchronized HttpClient getHttpClient(){
        if(httpClient==null){
            final HttpParams httpParams =new BasicHttpParams();

            //设置获取连接池超时
            ConnManagerParams.setTimeout(httpParams, DEFAULT_TIMEOUT);
            //设置访问服务器超时
            HttpConnectionParams.setConnectionTimeout(httpParams, DEFAULT_SOCKET_TIMEOUT);
            //设置服务器返回信息超时
            HttpConnectionParams.setSoTimeout(httpParams, DEFAULT_SOCKET_TIMEOUT);

            //设置路由最大连接数
            ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(DEFAULT_HOST_CONNECTIONS));
            //设置最大连接数
            ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

            //设置先发送请求头，如果通过再继续
            HttpProtocolParams.setUseExpectContinue(httpParams, true);
            //设置是否使用重用连接，如果不使用可能会出现IO异常，默认使用但会花费时间10-30ms
            HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);

            // set user agent
//            String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
//            HttpProtocolParams.setUserAgent(httpParams, userAgent);

            // 设置不使用Nagle算法，增加效率和带宽消耗
            HttpConnectionParams.setTcpNoDelay(httpParams, true);
            HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);

            // scheme: http and https
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

            ClientConnectionManager manager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

            httpClient=new DefaultHttpClient(manager,httpParams);
        }
        return httpClient;
    }

    public static synchronized HttpPost getHttpDefaultPost(String url){
        defaultHttpPost=new HttpPost(url);
        return  defaultHttpPost;
    }

    public static synchronized HttpPost getHttpGzipPost(String url){
        if(gZipHttpPost==null){
            gZipHttpPost=new HttpPost(url);
            gZipHttpPost.setHeader("Accept-Encoding","gzip");
        }
        return  gZipHttpPost;
    }
}
